package com.gehj.alluitls;

import android.text.format.DateUtils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

//import java.lang.management.ManagementFactory;

/**
 * 时间工具类
 * Created by Administrator on 2019-04-26.
 */
public class DateUtil_gehj extends DateUtils{

    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    
    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};


    // 各种时间格式
    public static final SimpleDateFormat date_sdf = new SimpleDateFormat(
            "yyyy-MM-dd");
    // 各种时间格式
    public static final SimpleDateFormat yyyyMMdd = new SimpleDateFormat(
            "yyyyMMdd");
    // 各种时间格式
    public static final SimpleDateFormat date_sdf_wz = new SimpleDateFormat(
            "yyyy年MM月dd日");
    public static final SimpleDateFormat time_sdf = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    public static final SimpleDateFormat yyyymmddhhmmss = new SimpleDateFormat(
            "yyyyMMddHHmmss");
    public static final SimpleDateFormat short_time_sdf = new SimpleDateFormat(
            "HH:mm");
    public static final SimpleDateFormat datetimeFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat monthFormat = new SimpleDateFormat(
            "MM");
    public static final SimpleDateFormat yearMonthFormat = new SimpleDateFormat(
            "yyyy-MM");
    public static final SimpleDateFormat minuteFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");

    public static final SimpleDateFormat yearAndMonthFormat = new SimpleDateFormat(
            "yyyy年MM月");
    /**
     * 获取当前Date型日期
     * 
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     * 
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    /*public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }*/
    
    /**
     * 获取服务器启动时间
     */
    /*public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime(); //android不用jvm这个方法没法用;
        return new Date(time);
    }*/

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }


    /**
     * 返回指定日期所在月份的第一天
     *
     * @param date
     * @return
     */
    public static String getMonthFirstDay(String date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(str2Date(date, datetimeFormat));
        int firstDay = calendar.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }


    /**
     * 返回指定日期所在月份的第一天
     *
     * @param date
     * @return
     */
    public static String getMonthFirstDay(String date,SimpleDateFormat dateFormat) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(str2Date(date, dateFormat));
        int firstDay = calendar.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    /**
     * 返回指定日期所在月份的最后一天
     *
     * @param date
     * @return
     */
    public static String getMonthLastDay(String date,SimpleDateFormat dateFormat) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(str2Date(date, dateFormat));
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }


    /**
     * 返回指定日期所在月份的最后一天
     *
     * @param date
     * @return
     */
    public static String getMonthLastDay(String date) {
        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTime(str2Date(date, datetimeFormat));
        int lastDay = calendar.getActualMaximum(Calendar.DATE);
        //设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }

    /**
     * 字符串转换成日期
     *
     * @param str
     * @param sdf
     * @return
     */
    public static Date str2Date(String str, SimpleDateFormat sdf) {
        if (null == str || "".equals(str)) {
            return null;
        }
        Date date = null;
        try {
            date = sdf.parse(str);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 日期转换为字符串
     *
     * @param date_sdf 日期格式
     * @return 字符串
     */
    public static String date2Str(SimpleDateFormat date_sdf) {
        Date date = getDateNow();
        if (null == date) {
            return null;
        }
        return date_sdf.format(date);
    }

    /**
     * 当前日期
     *
     * @return 系统当前时间
     */
    public static Date getDateNow() {
        return new Date();
    }



    public static String date2St_timePicker(Date date) {
        if (null == date) {
            return "";
        }
        return datetimeFormat.format(date);
    }

    /**
     * date转分-->string
     * @param date
     * @return
     */
    public static String date2StMin_timePicker(Date date) {
        if (null == date) {
            return "";
        }
        return minuteFormat.format(date);
    }

    /**
     * 字符串转month
     */

    /**
     * 字符串转换成月
     *
     * @param str
     * @param sdf
     * @return
     */
    public static int str2Month(String str, SimpleDateFormat sdf) {
        if (null == str || "".equals(str)) {
            return 0;
        }
        Date date = null;
        try {
            date = sdf.parse(str);
            return date.getMonth() + 1;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
